<template>
  <el-dialog
    :visible.sync="isShow"
    :title="{ add: '新增学科', edit: '编辑学科' }[mode]"
    @close="closeEvent"
  >
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="学科编号" prop="rid">
        <el-input v-model="form.rid"></el-input>
      </el-form-item>
      <el-form-item label="学科名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="学科简称" prop="short_name">
        <el-input v-model="form.short_name"></el-input>
      </el-form-item>
      <el-form-item label="学科简介" prop="intro">
        <el-input v-model="form.intro"></el-input>
      </el-form-item>
      <el-form-item label="学科备注" prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align:center">
        <el-button @click="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { subjectAdd, subjectEdit } from '../../../api/subject'
export default {
  data () {
    return {
      isShow: false,
      form: {
        rid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      rules: {
        rid: [{ required: true, message: '必填', trigger: 'change' }],
        name: [{ required: true, message: '必填', trigger: 'change' }]
      },
      mode: 'add'
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async reslove => {
        if (reslove) {
          if (this.mode === 'add') {
            await subjectAdd(this.form)
            this.$message.success('新增成功')
          } else {
            await subjectEdit(this.form)
            this.$message.success('编辑成功')
          }
          this.isShow = false
          this.$emit('getDate')
        } else {
        }
      })
    },
    closeEvent () {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        rid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-dialog__header {
  background: linear-gradient(#01c3fa, #1197fa);
  text-align: center;
}
/deep/.el-dialog__title {
  color: white;
}
/deep/.el-dialog__close {
  color: white;
}
</style>
